// pages/kin/kin.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            list:[]
      },
      addKin(){
            wx.navigateTo({
                  url: '/pages/addKin/addKin',
            })
      },

      itemTap(e){
            let id=e.currentTarget.dataset.id;
            wx.showActionSheet({
                  itemList: ['编辑', '删除'],
                  success:res=>{
                        if(res.tapIndex==0){
                              wx.navigateTo({
                                    url: '/pages/addKin/addKin?id='+id
                              })
                        }else if(res.tapIndex==1){
                              this.delKin(id);
                        }
                  },
                  fail (res) {
                        console.log(res.errMsg)
                  }
            })
      },
      delKin(id){
            wx.showLoading({
                  title: '删除联系人',
            })
            wx.request({
                  url: getApp().globalData.host+'client/user/delContact',
                  header:{
                        'content-type': 'application/x-www-form-urlencoded'
                  },
                  method:'DELETE',
                  data:{
                        id:id
                  },
                  success:()=>{
                        wx.showToast({
                              title: '删除成功',
                        })
                        this.init();
                  },
                  fail:()=>{
                        wx.showToast({
                              title: '服务器繁忙',
                              icon:'error'
                        })
                  },
                  complete:()=>{
                        wx.hideLoading()
                  }
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {

      },


      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            let userInfo=wx.getStorageSync("userInfo");
            if(userInfo.nickName==null || userInfo.nickName==""){
                  wx.showModal({
                        title: '未登录',
                        content: '是否进行登录？',
                        success: (res)=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url:'/pages/login/login'
                                    })
                              } else if (res.cancel) {
                                    // 点击取消后返回主页面
                                    wx.switchTab({
                                          url: '/pages/index/index'
                                    })
                                    wx.showToast({
                                          title: '需要登录',
                                          icon: 'error'
                                    })
                              }
                        }
                  })
            }else if(userInfo.addr == undefined || userInfo.addr == "null" || userInfo.addr == "" || userInfo.addr == null){
                  wx.showModal({
                        title: '未完善信息',
                        content: '需要补全房屋信息',
                        success: (res)=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url:'/pages/userInfo/userInfo'
                                    })
                              } else if (res.cancel) {
                                    // 点击取消后返回主页面
                                    wx.switchTab({
                                          url: '/pages/index/index'
                                    })
                                    wx.showToast({
                                          title: '需要完善信息',
                                          icon: 'error'
                                    })
                              }
                        }
                  })
            }else{
                  this.init();
            }
      },
      init(){
            wx.showLoading({
              title: '加载数据'
            })
            wx.request({
                  url: getApp().globalData.host+'client/user/ContactsList?userId='+wx.getStorageSync('userId'),
                  method:'GET',
                  success:res=>{
                        let contactsList=res.data.data.contactsList
                        contactsList.forEach(item=>{
                              let time=new Date(item.time);
                              let m=(time.getMonth()+1)<10?('0'+(time.getMonth()+1)):(time.getMonth()+1);
                              let d=time.getDate()<10?'0'+time.getDate():time.getDate();
                              item.date=m+'-'+d
                        })
                        this.setData({
                              list:res.data.data.contactsList
                        })
                  },
                  fail:()=>{
                        wx.showToast({
                          title: '服务器繁忙',
                          icon:'error'
                        })
                  },
                  complete:()=>{
                        wx.hideLoading()
                  }
            })
      }
})